package be.kuleuven.cs.mop.app.controllers;


import java.util.ArrayList;
import java.util.List;

import be.kuleuven.cs.mop.app.System;
import be.kuleuven.cs.mop.domain.exceptions.InvalidCurrentUserException;
import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.Project;
import be.kuleuven.cs.mop.domain.model.Task;
import be.kuleuven.cs.mop.domain.model.impl.ProjectImpl;
import be.kuleuven.cs.mop.domain.model.impl.TaskImpl;



/**
 * The {@link Controller} responsible for handling {@link Project} operations
 */
public class ProjectController extends Controller {
	
	/**
	 * Creates a new ProjectController
	 * @param system - The <code>System</code>
	 */
	public ProjectController(final System system) {
		super(system);
	}
	
	
	
	/**
	 * Creates a new {@link Project}
	 * @param description - The description for the <code>Project</code>
	 * 
	 * @return The new {@link Project}
	 * 
	 * @throws TaskManagerException PROJECT_INVALID_DESCRIPTION - !{@link ProjectImpl#isValidDescription(String)}
	 * @throws InvalidCurrentUserException NO_USER_LOGGED_IN - {@link #checkIfUserIsLoggedIn()}
	 */
	public Project createProject(final String description) throws TaskManagerException, InvalidCurrentUserException {
		checkIfUserIsLoggedIn();
		return new ProjectImpl(getWorld(), description);
	}
	
	/**
	 * Assigns the specified {@link Task} to the specified {@link Project}
	 * @param task 		- The <code>Task</code>
	 * @param project 	- The <code>Project</code>
	 * 
	 * @throws TaskManagerException INVALID_TASK - !{@link #isValidTask}
	 * @throws InvalidCurrentUserException NO_USER_LOGGED_IN - {@link #checkIfUserIsLoggedIn()}
	 */
	public void assignTaskToProject(final Task task, final Project project) throws TaskManagerException, InvalidCurrentUserException {
		checkIfUserIsLoggedIn();
		((ProjectImpl)project).assignTask((TaskImpl)task);
	}
	
	/**
	 * Returns an <code>List</code> of all existing {@link Project}s
	 * @throws InvalidCurrentUserException NO_USER_LOGGED_IN - {@link #checkIfUserIsLoggedIn()}
	 */
	public List<Project> getProjects() throws InvalidCurrentUserException {
		checkIfUserIsLoggedIn();
		return new ArrayList<Project>(getWorld().getProjects());
	}
	
	/**
	 * Removes the specified {@link Project}
	 * Also removes all the {@link Task}s that the given {@link Project} contained, non-recursively
	 * @param project - The <code>Project</code>
	 * 
	 * @throws InvalidCurrentUserException NO_USER_LOGGED_IN - {@link #checkIfUserIsLoggedIn()}
	 */
	public void removeProject(final Project project) throws TaskManagerException, InvalidCurrentUserException {
		checkIfUserIsLoggedIn();
		((ProjectImpl)project).remove();
	}
}